#include "stm32f10x.h"                  // Device header
#include "myspi.h" 
#include "OLED_SPI.h" 
#include "2_0TFTSPI_.h"
#include "Delay.h" 
 #include "words.h" 
	void OLED_clear(void)
  {
    unsigned int ROW,column;
	TFT_SEND_CMD(0x2a); 		//Column address set
	TFT_SEND_DATA(0x00); 		//start column
	TFT_SEND_DATA(0x00); 
	TFT_SEND_DATA(0x00);		//end column
	TFT_SEND_DATA(0xEF);

	TFT_SEND_CMD(0x2b); 		//Row address set
	TFT_SEND_DATA(0x00); 		//start row
	TFT_SEND_DATA(0x00); 
	TFT_SEND_DATA(0x01);		//end row
	TFT_SEND_DATA(0x3F);
	TFT_SEND_CMD(0x2C);			//Memory write
    for(ROW=0;ROW<OLED_LINE_NUMBER;ROW++)             //ROW loop
      { 
	  
          for(column=0;column<OLED_COLUMN_NUMBER;column++)	//column loop
            {
              TFT_SEND_DATA(0x00);
			  TFT_SEND_DATA(0x00);
				TFT_SEND_DATA(0x00);
            }
      }
  }
void OLED_full(unsigned long color)
  {
    unsigned int ROW,column;
	  TFT_SEND_CMD(0x2a); 		//Column address set
	TFT_SEND_DATA(0x00); 		//start column
	TFT_SEND_DATA(0x00); 
	TFT_SEND_DATA(0x00);		//end column
	TFT_SEND_DATA(0xEF);

	TFT_SEND_CMD(0x2b); 		//Row address set
	TFT_SEND_DATA(0x00); 		//start row
	TFT_SEND_DATA(0x00); 
	TFT_SEND_DATA(0x01);		//end row
	TFT_SEND_DATA(0x3F);
	  TFT_SEND_CMD(0x2C);			//Memory write
    for(ROW=0;ROW<OLED_LINE_NUMBER;ROW++)             //ROW loop
      { 
	  
	for(column=0;column<OLED_COLUMN_NUMBER ;column++)	//column loop
          {
            TFT_SEND_DATA(color>>16);
			  TFT_SEND_DATA(color>>8);
			  TFT_SEND_DATA(color);
          }
      }
  }
	void OLED_init(void)				////ST7789V2//此函数已经包含了spi的初始化，后续不必再初始化
  {
		Else_Init();//改为自己的初始化函数即可
		TFT_RST();//同上
		Delay_ms(1000);
    TFT_SEND_CMD(0x11); 			//Sleep Out
		Delay_ms(120);               //DELAY120ms 
	//--------------------------------ST7789S Frame rate setting----------------------------------// 
	TFT_SEND_CMD(0x2a); 		//Column address set
	TFT_SEND_DATA(0x00); 		//start column
	TFT_SEND_DATA(0x00); 
	TFT_SEND_DATA(0x00);		//end column
	TFT_SEND_DATA(0xef);

	TFT_SEND_CMD(0x2b); 		//Row address set
	TFT_SEND_DATA(0x00); 		//start row
	TFT_SEND_DATA(0x28); 
	TFT_SEND_DATA(0x01);		//end row
	TFT_SEND_DATA(0x17);

	TFT_SEND_CMD(0xb2); 		//Porch control
	TFT_SEND_DATA(0x0c); 
	TFT_SEND_DATA(0x0c); 
	TFT_SEND_DATA(0x00); 
	TFT_SEND_DATA(0x33); 
	TFT_SEND_DATA(0x33); 

	TFT_SEND_CMD(0x20); 		//Display Inversion Off

	TFT_SEND_CMD(0xb7); 		//Gate control
	TFT_SEND_DATA(0x56);   		//35
//---------------------------------ST7789S Power setting--------------------------------------// 
	TFT_SEND_CMD(0xbb); //VCOMS Setting
	TFT_SEND_DATA(0x18);  //1f

	TFT_SEND_CMD(0xc0); 		//LCM Control
	TFT_SEND_DATA(0x2c); 

	TFT_SEND_CMD(0xc2); 		//VDV and VRH Command Enable
	TFT_SEND_DATA(0x01); 

	TFT_SEND_CMD(0xc3); //VRH Set
	TFT_SEND_DATA(0x1f); //12

	TFT_SEND_CMD(0xc4); 			//VDV Setting
	TFT_SEND_DATA(0x20); 

	TFT_SEND_CMD(0xc6); 			//FR Control 2
	TFT_SEND_DATA(0x0f); 
//TFT_SEND_CMD(0xca); 
//TFT_SEND_DATA(0x0f); 
//TFT_SEND_CMD(0xc8); 
//TFT_SEND_DATA(0x08); 
//TFT_SEND_CMD(0x55); 
//TFT_SEND_DATA(0x90); 
	TFT_SEND_CMD(0xd0);  //Power Control 1
	TFT_SEND_DATA(0xa6);   //a4
	TFT_SEND_DATA(0xa1); 
//--------------------------------ST7789S gamma setting---------------------------------------// 

	TFT_SEND_CMD(0xe0); 
	TFT_SEND_DATA(0xd0); 
	TFT_SEND_DATA(0x0d); 
	TFT_SEND_DATA(0x14); 
	TFT_SEND_DATA(0x0b); 
	TFT_SEND_DATA(0x0b); 
	TFT_SEND_DATA(0x07); 
	TFT_SEND_DATA(0x3a);  
	TFT_SEND_DATA(0x44); 
	TFT_SEND_DATA(0x50); 
	TFT_SEND_DATA(0x08); 
	TFT_SEND_DATA(0x13); 
	TFT_SEND_DATA(0x13); 
	TFT_SEND_DATA(0x2d); 
	TFT_SEND_DATA(0x32); 

	TFT_SEND_CMD(0xe1); 				//Negative Voltage Gamma Contro
	TFT_SEND_DATA(0xd0); 
	TFT_SEND_DATA(0x0d); 
	TFT_SEND_DATA(0x14); 
	TFT_SEND_DATA(0x0b); 
	TFT_SEND_DATA(0x0b); 
	TFT_SEND_DATA(0x07); 
	TFT_SEND_DATA(0x3a); 
	TFT_SEND_DATA(0x44); 
	TFT_SEND_DATA(0x50); 
	TFT_SEND_DATA(0x08); 
	TFT_SEND_DATA(0x13); 
	TFT_SEND_DATA(0x13); 
	TFT_SEND_DATA(0x2d); 
	TFT_SEND_DATA(0x32);
	
	TFT_SEND_CMD(0x36); 			//Memory data access control
	TFT_SEND_DATA(0x00); 
	
	TFT_SEND_CMD(0x3A); 			//Interface pixel format
	//TFT_SEND_DATA(0x55);			//65K	
	TFT_SEND_DATA(0x66);			//262K  RGB 6 6 6

	TFT_SEND_CMD(0xe7); 			//SPI2 enable    ƴӃ2ʽ¾ݍ¨µÀģʽ
	TFT_SEND_DATA(0x00); 


	TFT_SEND_CMD(0x21);			//Display inversion on
	TFT_SEND_CMD(0x29); 			//Display on
	TFT_SEND_CMD(0x2C);			//Memory write
  }
	
	
	/**
	*@brief图片显示，竖屏显示,编码格式为24位真彩色!!!!!
     *@paramconst unsigned char *ptr_pic,uint16_t：图片数组首地址，
									（uint16_t CS,uint16_t CE）：图片的起始列和终止列，注意，在两个数的差值要等于图片长度
									（uint16_t RS,uint16_t RE）：图片的起始行和终止行，注意，在两个数的差值要等于图片宽度
     *@retval
     */
	
void Picture_display(const unsigned char *ptr_pic,uint16_t CS,uint16_t CE,uint16_t RS,uint16_t RE)
  {//这个程序的核心思想是用arrin[4]来存输入的两字节数据，每个元素转化为arrout[8]存储的高低各一字节数据，分别发送给函数TFT_SEND_DATA（）
		uint16_t arrin[4]=0,arrout[8]=0,k=0;
		unsigned int ROW,column;
		for(int i=0,j=0;i<4;i++)
		{
			switch (i)//用于将不同数据放到数组里去
				{
					case 0:arrin[i]=CS;break;
					case 1:arrin[i]=CE-1;break;
					case 2:arrin[i]=RS;break;
					case 3:arrin[i]=RE-1;break;
				}
			arrout[j++]=(arrin[i]>>8)&0xff;
			arrout[j++]=arrin[i]&0xff;
		}
		
	TFT_SEND_CMD(0x2a); 		//Column address set
	TFT_SEND_DATA(arrout[k++]); 		//start column设置起始点的起始列
	TFT_SEND_DATA(arrout[k++]); 
	TFT_SEND_DATA(arrout[k++]);		//end column设置终止点的列，而且要求与图像的长宽对应
	TFT_SEND_DATA(arrout[k++]);   //如果长是40，这里就要填40-1,所以前面的数组才会有减一的操作

	TFT_SEND_CMD(0x2b); 		//Row address set
	TFT_SEND_DATA(arrout[k++]); 		//start row设置起始点的行
	TFT_SEND_DATA(arrout[k++]); 
	TFT_SEND_DATA(arrout[k++]);		//end row设置终止点的行
	TFT_SEND_DATA(arrout[k++]);
	  TFT_SEND_CMD(0x2C);			//Memory write
		k=0;//结束清0
		//以上内容是告诉模块将在哪个区域显示，模块会自动做好元素的换行，下面只要填入数据就行
    for(ROW=0;ROW<(arrin[1]-arrin[0]+1);ROW++)        //ROW loop，两重for循环的中止条件要和分辨率对应（就是图片的长和宽）
      {   
		
				for(column=0;column<(arrin[3]-arrin[2]+1);column++)	//column loop
          {
            TFT_SEND_DATA(*ptr_pic++);
						TFT_SEND_DATA(*ptr_pic++);
						TFT_SEND_DATA(*ptr_pic++);
          }
      }
  }
	
		void display_char32_16(unsigned int x,unsigned int y,unsigned long color,const unsigned char *point)//字符32×16显示
{
	 unsigned int column;
	unsigned char tm=0,temp;
	 TFT_SEND_CMD(0x2a); 		//Column address set
	TFT_SEND_DATA(x>>8); 		//start column
	TFT_SEND_DATA(x);
	x=x+15;
	TFT_SEND_DATA(x>>8);		//end column
	TFT_SEND_DATA(x);

	TFT_SEND_CMD(0x2b); 		//Row address set
	TFT_SEND_DATA(y>>8); 		//start row
	TFT_SEND_DATA(y); 
	y=y+31;
	TFT_SEND_DATA(y>>8);		//end row
	TFT_SEND_DATA(y);
	 TFT_SEND_CMD(0x2C);			//Memory write
	for(column=0;column<64;column++)	//column loop
          {
						//for(uint8_t hang=0;hang<2;hang++)//加了这一句和temp=point[column*2+hang]才能够显示32*16（否则是显示16*16），我猜测是数组只遍历了一半
					//{	temp=point[column*2+hang];
						temp=*point;
							for(tm=0;tm<8;tm++)//用temp&0x01将temp里的数据一个一个进行拆分，颜色在写入函数的时候就已经选定，只考虑当前这个像素里昂或不亮的问题
					{
						if(temp&0x01)//如果当前位结果为一，就亮
						{
						TFT_SEND_DATA(color>>16);//处理颜色
						TFT_SEND_DATA(color>>8);
						TFT_SEND_DATA(color);
						}
						else //否则就不亮
						{
						TFT_SEND_DATA(0);
						TFT_SEND_DATA(0);
						TFT_SEND_DATA(0);
						}
						temp>>=1;
					}*point++;
				
      }
}

void display_char32_32(unsigned int x,unsigned int y,unsigned long color,const unsigned char *point)//字符32×32显示
{
	 unsigned int column;
	unsigned char tm=0,temp;
	 TFT_SEND_CMD(0x2a); 		//Column address set
	TFT_SEND_DATA(x>>8); 		//start column
	TFT_SEND_DATA(x);
	x=x+31;
	TFT_SEND_DATA(x>>8);		//end column
	TFT_SEND_DATA(x);

	TFT_SEND_CMD(0x2b); 		//Row address set
	TFT_SEND_DATA(y>>8); 		//start row
	TFT_SEND_DATA(y); 
	y=y+31;
	TFT_SEND_DATA(y>>8);		//end row
	TFT_SEND_DATA(y);
	 TFT_SEND_CMD(0x2C);			//Memory write
	for(column=0;column<128;column++)	//column loop,一个字节控制八个像素，128个控制1024个像素，也就是32*32
          {
					
						temp=*point;
							for(tm=0;tm<8;tm++)//用temp&0x01将temp里的数据一个一个进行拆分，颜色在写入函数的时候就已经选定，只考虑当前这个像素里昂或不亮的问题
					{
						if(temp&0x01)//如果当前位结果为一，就亮
						{
						TFT_SEND_DATA(color>>16);//处理颜色
						TFT_SEND_DATA(color>>8);
						TFT_SEND_DATA(color);
						}
						else //否则就不亮
						{
						TFT_SEND_DATA(0);
						TFT_SEND_DATA(0);
						TFT_SEND_DATA(0);
						}
						temp>>=1;
					}*point++;
				//}
      }
}

void display_char8_16(unsigned int x,unsigned int y,unsigned long color, char car)//字符8×16显示
{	const unsigned char *point;
	 unsigned int column;
	unsigned char tm=0,temp;
	point=Ascii[car-32];
	 TFT_SEND_CMD(0x2a); 		//Column address set
	TFT_SEND_DATA(x>>8); 		//start column
	TFT_SEND_DATA(x);
	x=x+7;
	TFT_SEND_DATA(x>>8);		//end column
	TFT_SEND_DATA(x);

	TFT_SEND_CMD(0x2b); 		//Row address set
	TFT_SEND_DATA(y>>8); 		//start row
	TFT_SEND_DATA(y); 
	y=y+15;
	TFT_SEND_DATA(y>>8);		//end row
	TFT_SEND_DATA(y);
	 TFT_SEND_CMD(0x2C);			//Memory write
	for(column=0;column<16;column++)	//column loop,一个字节控制八个像素，128个控制1024个像素，也就是32*32
          {
					
						temp=*point;
							for(tm=0;tm<8;tm++)//用temp&0x01将temp里的数据一个一个进行拆分，颜色在写入函数的时候就已经选定，只考虑当前这个像素里昂或不亮的问题
					{
						if(temp&0x01)//如果当前位结果为一，就亮
						{
						TFT_SEND_DATA(color>>16);//处理颜色
						TFT_SEND_DATA(color>>8);
						TFT_SEND_DATA(color);
						}
						else //否则就不亮
						{
						TFT_SEND_DATA(0);
						TFT_SEND_DATA(0);
						TFT_SEND_DATA(0);
						}
						temp>>=1;
					}*point++;
				//}
      }
}


void display_string8_16(unsigned int x,unsigned int y,unsigned long color, char *string)//字符8×16显示
{	uint16_t i=0;
	while(string[i]!='\0')
	{display_char8_16(x,y,color,string[i]);
		x+=8;
		i++;
	}
}

void display_NUmber8_16(unsigned int x,unsigned int y,unsigned long color,unsigned int number,uint8_t wei)//字符8×16显示
{
	unsigned int temp=number;
	while(wei)
	{
		
			display_char8_16(x+wei*8,y,color,temp%10+'0');
			temp/=10;
			wei-=1;
	}
	
	

}
